//
//  SEGWindmillRefreshCommonHeader.m
//  RefreshDemo
//
//  Created by Clavis on 2019/1/4.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "SEGWindmillRefreshCommonHeader.h"

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#import <Masonry/Masonry.h>

//箭头颜色
#define kARrowColor kColor_T


@interface SEGWindmillRefreshCommonHeader ()
/** 所有状态对应的文字 */
@property (strong, nonatomic) NSMutableDictionary *stateTitles;
@end

@implementation SEGWindmillRefreshCommonHeader

#pragma mark - 公共方法
- (void)setTitle:(NSString *)title forState:(SEGRefreshState)state {
    if (title == nil) return;
    self.stateTitles[@(state)] = title;
    self.stateLabel.text = self.stateTitles[@(self.state)];
}

#pragma mark - 私有方法
- (void)addCircleAnimation {
    self.circleLayer.strokeStart = 0.1;
    
    CABasicAnimation *animatioin = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animatioin.fromValue = @(0.0);
    animatioin.toValue = @(M_PI * 2);
    animatioin.duration = 0.5;
    animatioin.autoreverses = NO;
    animatioin.fillMode = kCAFillModeForwards;
    animatioin.repeatCount = MAXFLOAT;
    [self.circleLayerView.layer addAnimation:animatioin forKey:@"rotate"];
    
    [self.arrowLayer removeFromSuperlayer];
}

- (void)stopCirCleAnimation {
    self.circleLayer.strokeStart = 0.0;
    [self.circleLayerView.layer removeAnimationForKey:@"rotate"];
    
    [self.circleView.layer addSublayer:self.arrowLayer];
}

#pragma mark - 覆盖父类的方法
- (void)prepare {
    [super prepare];
    
    self.stateTitles = [NSMutableDictionary dictionary];
    self.stateLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        lable.textColor = kColor_3;
        lable.font = [UIFont boldSystemFontOfSize:11];
        lable;
    });
    [self addSubview:self.stateLabel];
    
    // 初始化文字
    [self setTitle:@"下拉即可刷新" forState:SEGRefreshStateIdle];
    [self setTitle:@"释放即可刷新" forState:SEGRefreshStatePulling];
    [self setTitle:@"正在刷新..." forState:SEGRefreshStateRefreshing];
    
//    self.layer.borderColor = UIColor.redColor.CGColor;
//    self.layer.borderWidth = 1;
    
    // 初始化圆圈
    self.circleView = [[UIView alloc] init];
    self.circleView.frame = CGRectMake(60, 15, 24, 24);
    self.circleView.backgroundColor = UIColor.clearColor;
    [self addSubview:self.circleView];
    
    self.circleLayerView = [[UIView alloc] initWithFrame:self.circleView.bounds];
    self.circleLayerView.backgroundColor = UIColor.clearColor;
    [self.circleView addSubview:self.circleLayerView];
    
    self.circleLayer = [[CAShapeLayer alloc] init];
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(12, 12) radius:12 startAngle:(-0.5*M_PI) endAngle:1.5*M_PI clockwise:YES];
    self.circleLayer.path = path.CGPath;
    self.circleLayer.strokeColor = kColor_3.CGColor;
    self.circleLayer.fillColor = UIColor.clearColor.CGColor;
    self.circleLayer.lineWidth = 1;
    [self.circleLayerView.layer addSublayer:self.circleLayer];
    self.circleLayer.strokeEnd = 0.0;
    
    // 初始化箭头
    CGFloat radius = 12;
    CGFloat height = 24;
    UIBezierPath * pathArrow = [UIBezierPath bezierPath];
    pathArrow.lineWidth = 1.0;
    [pathArrow moveToPoint:CGPointMake(radius, 5)];
    [pathArrow addLineToPoint:CGPointMake(radius, height-3*2)];
    [pathArrow stroke];
    [pathArrow moveToPoint:CGPointMake(radius-6, height-3*2-5)];
    [pathArrow addLineToPoint:CGPointMake(radius, height-3*2)];
    [pathArrow addLineToPoint:CGPointMake(radius+6, height-3*2-5)];
    [pathArrow stroke];
    CAShapeLayer *layerArrow = [[CAShapeLayer alloc] init];
    layerArrow.path = pathArrow.CGPath;
    layerArrow.backgroundColor = [UIColor clearColor].CGColor;
    layerArrow.fillColor = [UIColor clearColor].CGColor;
    layerArrow.strokeColor = kARrowColor.CGColor;
    layerArrow.lineWidth = 1.0;
    self.arrowLayer = layerArrow;
    [self.circleView.layer addSublayer:layerArrow];
    
    // 添加约束
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(10);
        make.centerY.equalTo(self);
    }];
    
    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.stateLabel);
        make.right.equalTo(self.stateLabel.mas_left).mas_offset(-10);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
}

- (void)placeSubviews {
    [super placeSubviews];
    
    if (self.stateLabel.hidden) return;
}

- (void)setState:(SEGRefreshState)state {
    SEGRefreshCheckState
    
    // 设置状态文字
    self.stateLabel.text = self.stateTitles[@(state)];
    
    // 开启或结束动画
    if (state == SEGRefreshStateRefreshing) {
        [self addCircleAnimation];
    } else {
        [self stopCirCleAnimation];
    }
}

- (void)setPullingPercent:(CGFloat)pullingPercent {
    [super setPullingPercent:pullingPercent];
    
    if (self.isRefreshing) return;
    self.circleLayer.strokeEnd = self.pullingPercent;
}

@end
